import React from "react";
import {Button, Col, Row, Form} from "antd";
import PlatformMenuSelect from "../pmenu/PlatformMenuSelect";
import PlatformSelect from "../platform/PlatformSelect";
import RoleSelect from "../role/RoleSelect";
import BaseComponent from "../../../../redare/components/BaseComponent";
import UserPresenter from "../../.gen/presenter/component/core/UserPresenter";

export default class UserPlatformForm extends BaseComponent {

    static defaultProps = {
        onSuccess: undefined
    };

    constructor(props) {
        super(props);
        this.state = {data: {}};
        this.presenter = new UserPresenter(this);
    }

    componentDidMount() {
        this.presenter.getUser({id: this.props.id}, {}, (result) => {
            this.form.setFieldsValue({
                platform: result.data.platform
            });
            this.setState({data: result.data});
        });
    }

    _closeForm() {
        if (this.props.onCancel) {
            this.props.onCancel();
        }
    }

    _onSubmit() {
        this.form.validateFields().then(values => {
            this.showDoingIndicator('处理中');
            this.presenter.modifyPlatform({accountId: this.props.id, ...values}, {}, () => {
                this.noticeSuccess("修改成功");
                if (this.props.onSuccess) {
                    this.props.onSuccess();
                }
            });
        });
    }

    showLoadingIndicator(text) {
        this.showSpin(text);
    }

    closeLoadingIndicator() {
        this.closeSpin();
    }

    showDoingIndicator(text) {
        this.showSpin(text);
    }

    closeDoingIndicator() {
        this.closeSpin();
    }

    render() {
        const formItemLayout = {
            labelCol: {
                span: 4
            },
            wrapperCol: {
                span: 20
            },
        };
        return this.renderSpin(
            <Form ref={(o) => this.form = o} className="form" {...formItemLayout}>
                <Form.Item label="所属平台" name="platform" rules={[{required: true, message: '请选择所属平台！'}]}
                           trigger="onValueChange" validateTrigger="onValueChange">
                    <PlatformSelect onValueChange={(value) => {
                        this._form.platform = value;
                        this.refreshState();
                    }}/>
                </Form.Item>
                {
                    this._form.platform && this.state.data.platform !== this._form.platform && (
                        <Form.Item label="用户菜单" name="menuId" trigger="onValueChange" validateTrigger="onValueChange">
                            <PlatformMenuSelect platform={this._form.platform}/>
                        </Form.Item>
                    )
                }
                {
                    this._form.platform && this.state.data.platform !== this._form.platform && (
                        <Form.Item label="用户角色" name="roleIds" trigger="onValueChange" validateTrigger="onValueChange">
                            <RoleSelect platform={this._form.platform}/>
                        </Form.Item>
                    )
                }
                <Row type="flex" justify="end">
                    <Col>
                        <Button onClick={this._closeForm.bind(this)}>
                            取消
                        </Button>
                    </Col>
                    <Col offset={1}>
                        <Button type="primary" onClick={this._onSubmit.bind(this)}>
                            保存
                        </Button>
                    </Col>
                </Row>
            </Form>
        );
    }
}
